package com.egg.auth.config.token;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.hutool.core.util.ObjectUtil;
import com.egg.auth.rabbitmq.sender.AccessSender;
import com.egg.auth.service.login.LoginHelper;
import com.egg.auth.service.user.IUserService;
import com.egg.common.util.HttpServletUtil;
import com.egg.common.util.IpUtil;
import com.egg.logger.model.request.AccessPost;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义侦听器的实现
 *
 * @author east
 */
@Slf4j
@Configuration
public class SaTokenListenerConfig implements SaTokenListener {


    @Autowired
    private IUserService userService;
    @Autowired
    private AccessSender accessSender;


    /**
     * 发送一个登录日志
     *
     * @param userId
     */
    private void accessSenderOnLogin(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            return;
        }
        HttpServletRequest request = HttpServletUtil.getHttpServletRequest();
        if (null == request) {
            return;
        }
        AccessPost accessPost = new AccessPost();
        accessPost.setRemark("用户登录");
        accessPost.setUserId(userId);
        accessPost.setClientIp(IpUtil.requestIp(request));
        accessPost.setServerIp(IpUtil.serverIp());
        accessPost.setMethod(request.getMethod());
        accessPost.setPath(request.getRequestURI());
        accessPost.setHeaders(HttpServletUtil.parsingRequestHeader(request).toJSONString());
        accessSender.send(accessPost);
    }

    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        Long userId = LoginHelper.getUserId();
        userService.resetOnline(userId);
        accessSenderOnLogin(userId);
    }

    /**
     * 每次注销时触发
     */
    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        userService.resetOffline(LoginHelper.getUserId());
    }

    /**
     * 每次被踢下线时触发
     *
     * @param loginType  账号类别
     * @param loginId    账号id
     * @param tokenValue token值
     */
    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        userService.resetOffline(LoginHelper.getUserId());
    }

    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {

    }

    @Override
    public void doDisable(String s, Object o, String s1, int i, long l) {

    }

    @Override
    public void doUntieDisable(String s, Object o, String s1) {

    }

    @Override
    public void doOpenSafe(String s, String s1, String s2, long l) {

    }

    @Override
    public void doCloseSafe(String s, String s1, String s2) {

    }

    @Override
    public void doCreateSession(String id) {

    }

    @Override
    public void doLogoutSession(String id) {

    }

    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {

    }


}